#pragma once

#include <stdio.h>
#include <string>
#include "ZakScript/ScriptMgr.h"
#include "ZakScript/ScriptApi.h"

using namespace std;

namespace zak {

class ZAKSCRIPT_API ScriptFileStream {
public:
	bool Open(string &filename, string &mode);
	void Close();
	int Read(float &f);
	int Write(float &f);
	int Read(int &f);
	int Write(int &f);
	int Read(unsigned int &f);
	int Write(unsigned int &f);
	int Read(char &f);
	int Write(char &f);
	int Read(unsigned char &f);
	int Write(unsigned char &f);
	int Read(short &f);
	int Write(short &f);
	int Read(unsigned short &f);
	int Write(unsigned short &f);
	int Read(string &f, int size);
	int Write(string &f);

	long Length() { return (_file?_filelength(_fileno(_file)):0); }
	bool IsOpen() { return (_file?true:false); }

	bool Eof() { 
		return _eof; 
	}

	static bool RegisterScriptFileStream(char *className=NULL);

	ScriptFileStream();
	~ScriptFileStream();
private:
	static void ScriptFileStreamConstructor(ScriptFileStream *sfs){
		new(sfs) ScriptFileStream();
	}
	void ScriptFileStreamAddRef() {
		++_ref;
	}
	void ScriptFileStreamRelease() {
		if (--_ref == 0) {
			delete this;
		}
	}

	FILE *_file;
	int _ref;
	bool _eof;
};

}